package service

import (
	"context"
	"github.com/kardianos/service"
	log "github.com/sirupsen/logrus"
	"miao-va-host/daemon"
	"miao-va-host/daemon/signal"
)

// 管理器管理服务生命周期。
type manager struct {
	cancel     context.CancelFunc
	configFile *string
}

// Start starts the service in a separate go routine.
func (m *manager) Start(service.Service) error {
	log.Debug("manager Start1")
	log.Debug("manager run 1")
	ctx, cancel := context.WithCancel(context.Background())
	m.cancel = cancel
	ctx = signal.WithContextFunc(ctx, func() {
		log.Info("received signal, terminating process")
		cancel()
	})

	daem := daemon.NewDaemon()
	go daem.Run(ctx)
	log.Debug("manager run 2")
	log.Debug("manager Start2")
	return nil
}

// Stop stops the service.
func (m *manager) Stop(service.Service) error {
	log.Debug("manager Stop 1")
	m.cancel()
	log.Debug("manager Stop 2")
	return nil
}

//func (m *manager) run() {
//	log.Debug("manager run 1")
//	ctx, cancel := context.WithCancel(context.Background())
//	m.cancel = cancel
//	ctx = signal.WithContextFunc(ctx, func() {
//		log.Info("received signal, terminating process")
//		cancel()
//	})
//
//	daem := daemon.NewDaemon()
//	go daem.Run(ctx)
//	log.Debug("manager run 2")
//}
